package gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import bot.*;

/**
 * Creates a button that when pressed generates the demographic paragraph for a Wikipedia article then puts it into the paste buffer.
 * @author Jamison Bradley
 */
public class DemographicButton extends JButton {
	/**
	 * Default serialization
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * The text field containing the city name.
	 */
	private JTextField city;
	/**
	 * Used for generating the demographic paragraph.
	 */
	private PageGenerator page;
	/**
	 * The text field containing the date info.
	 */
	private JTextField date;
	
	/**
	 * Constructs the button and adds its needed action listener and function.
	 * @param textCity is a TextField containing the city name for demographic paragraph generation.
	 * @param textDate is the TextField containing the date of retrieval for citing purposes.
	 */
	public DemographicButton(JTextField textCity, JTextField textDate){
		super("Demographic Paragraph");
		city = textCity;
		date = textDate;
		page = new PageGenerator();
		
		addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg) {
				try{
					//make sure input is valid
					if (city.getText().equals("")){
						throw new IllegalArgumentException("Invalid city name");
					}
					City choosen = new City(city.getText(), BotGui.data);
					Util.setPasteBuffer(page.getDemographicsParagraph(choosen, date.getText()));
				}
				catch (IllegalArgumentException e){
					e.printStackTrace();
					JOptionPane.showMessageDialog(null, "Invaild city name");
				}
			}
		});
	}
}
